package com.banciyuan.bcywebview.biz.main.discover.topwork;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Card;

public class TopWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Card> mCardList;
    private int mType;
    private boolean has_footer;

    public TopWorkAdapter(Context mContext, List<Card> cardList, int type) {
        this(mContext, cardList, type, true);
    }

    public TopWorkAdapter(Context mContext, List<Card> cardList, int type, boolean has_footer) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        this.mCardList = cardList;
        this.mType = type;
        this.has_footer = has_footer;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == mCardList.size() + 1) {
            return 2;
        } else {
            return 1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new TopWorkBlankHolder(mInflater.inflate(R.layout.smooth_blank, null));
        } else if (viewType == 2) {
            return new TopWorkBlankHolder(mInflater.inflate(R.layout.smooth_blank, null));
        } else {
            return new TopWorkViewHolder(mInflater.inflate(R.layout.top_work_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == 0) {
            TopWorkBlankHolder holder = (TopWorkBlankHolder) viewHolder;
            holder.blank.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2px(12, mContext)));
        } else if (getItemViewType(position) == 2) {
            if (has_footer) {
                TopWorkBlankHolder holder = (TopWorkBlankHolder) viewHolder;
                holder.blank.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2px(36, mContext)));
            } else {
                TopWorkBlankHolder holder = (TopWorkBlankHolder) viewHolder;
                holder.blank.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2px(1, mContext)));
            }
        } else {
            TopWorkViewHolder holder = (TopWorkViewHolder) viewHolder;
            Card mItem = mCardList.get(position - 1);
            ImageLoader.getInstance().displayImage(mItem.getContentAddress(), holder.ivWorkCover, ImageOptions.options);
            holder.tvAuthorName.setText(mItem.getUName());
            if (mType == TopWorkActivity.COS_TYPE) {
                holder.tvAnhtorInfo.setText(mContext.getString(R.string.coser_role));
            } else if (mType == TopWorkActivity.ILLUST_TYPE) {
                holder.tvAnhtorInfo.setText(mContext.getString(R.string.draw_role));
            }
            OnClick gridListener = new OnClick(position - 1);
            holder.viewAuthorInfo.setOnClickListener(gridListener);
            holder.ivWorkCover.setOnClickListener(gridListener);
        }
    }

    @Override
    public int getItemCount() {
        return mCardList.size() + 2;
    }

    class OnClick implements OnClickListener {

        int position;

        public OnClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Card mCard = mCardList.get(position);
            switch (v.getId()) {
                case R.id.view_author_info:
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, mCard.getUid());
                    break;
                case R.id.iv_work_cover:
                    if (!TextUtils.isEmpty(mCard.getCp_id())) {
                        DetailDataHelper.gotoCosDetail(mContext, mCard.getRp_id(), mCard.getCp_id(), false);
                    } else {
                        DetailDataHelper.gotoDrawerDetail(mContext, mCard.getRp_id(), mCard.getDp_id(), false);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}